const fs = require('fs');
const path = require('path')
// const process = require('process');
//console.log(process.cwd())
// console.log(__dirname);
// console.log(__filename)
// console.log(fs.readFileSync('./1.md'));

// process.nextTick(() => {
//   console.log('nextTick callback');
// });
// console.log('scheduled');
// process.env.a = 'aaaa';
// console.log(process.env.a)
// console.log(path.resolve('/foo', '/bar', 'baz'))
// console.log(path.resolve('/foo', '///bar', 'baz'))
// console.log(path.join('/a','//b'))
// console.log(path.join('/a','//b', '///c')) //\a\b\c
// console.log(__dirname, __filename)
setTimeout(()=>{
  console.log('setTimeout')
})
fs.readFile('./1.md', ()=>{
  setTimeout(()=>{
    console.log('readFile')
  })
  setImmediate(()=>{
    console.log('setImmediate')
  })
})

// 自己路由系统
/**
 * 先匹配路由在路径
 */
class Layer{
  constructor(path,method) {
    this.path = path;
    this.method = method
  }
  match(path, method) {
    return this.path === path && this.method == method;
  }
}
class Router{
  get(path) {
  
  }
  compose(layers, ctx, next) {
    let dispatch = (i)=>{
      if(i == layers.layers) return next();
      let callback =  layers[i].callback;
    }
    return new Promise.resolve(callback(ctx,()=>dispatch(i+1)))
  }
  routes(){
    return async (ctx, next) =>{
      let path = ctx.path;
      let method = ctx.method;
      let layers = this.statck.filter(layer=>layer.match(path, method))
      this.compose(layers, ctx, next);
    }
  }
}


const arr = ['get','post'];
arr.forEach(method=>{
  Router.prototype[method] = function(path) {
    let layer = new Layer(path, method);
    this.statck.push(layer)
  }
})
